package com.zrrd.charStream.reader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author WanJl
 * @version 1.0
 * @title FileReaderDemo01
 * @description 文件字符输入流
 * @create 2024/5/26
 */
public class FileReaderDemo01 {
    public static void main(String[] args) {
        //创建文件字符输入流对象
        FileReader fr = null;
        try {
            fr=new FileReader("E:/users/abc.txt");
            //读取一个字符
//            int i = fr.read();
//            System.out.println((char) i);
            //循环读取多个字符
//            int ch;
//            while((ch=fr.read())!=-1){
//                System.out.print((char)ch);
//            }
            //循环+字符数组读取多个字符
            char[] cbuf=new char[1024];//创建一个字符数组
            int len;//设定长度
            while((len=fr.read(cbuf))!=-1){
                System.out.println(new String(cbuf,0,len));
            }


        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fr.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
